package com.longai.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import util.Page;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.gson.Gson;
import com.longai.model.Moment;
import com.longai.model.User;
import com.longai.service.MomentService;
import com.longai.service.UserService;
import com.longai.vo.MomentVo;

//author by miao

@Controller
@RequestMapping("moment")
public class MomentAction {

	@Autowired
	MomentService momentService;
	@Autowired
	UserService userService;
	
	String error = "0";
	// 添加心情接口
	//参数
	//返回moment成功，""失败
	@RequestMapping(value = "addMoment",method=RequestMethod.GET)
	@ResponseBody
	public Object addMoment(@RequestParam("userId")String userId,@RequestParam("momentContent")String momentContent) {
		
		try {
					
			Moment moment = new Moment();
			moment.setMomentContent(momentContent);
			Date currentTime = new Date();
			Long time = currentTime.getTime();
			moment.setMomentPubtime(time);
			User user = userService.findById(Integer.parseInt(userId));
			moment.setUser(user);
			momentService.save(moment);
			MomentVo momentVo = new MomentVo(moment);
			System.out.println(momentContent);
			return momentVo;
			
			

		} catch (Exception e) {	
			Moment moment = new Moment();
			System.out.println(e);
			return moment;
		}		
	}

	// 删除心情接口
	//参数momentId
	//成功返回1，失败返回0
	@RequestMapping(value = "deleteMoment",method=RequestMethod.GET)
	@ResponseBody
	public Object deleteMoment(@RequestParam("momentId")String momentId) {
		
		try {
			
			momentService.delete(momentService.findById(Integer.parseInt(momentId))); 
			return momentId;
		} catch (Exception e) {
			
			return error;
		}
		
	}

	// 查看某个心情接口
	//参数momentId某个心情的Id
	//成功返回momentVo,失败返回""
	@RequestMapping(value = "getMoment",method=RequestMethod.GET)
	@ResponseBody
	public Object getMoment(@RequestParam("momentId")String momentId) {
		
		try {
			
			
			Moment moment = momentService.findById(Integer.parseInt(momentId));		
			MomentVo momentVo = new MomentVo(moment);
			return momentVo;
		} catch (Exception e) {
			
			System.out.println(e);
			return "error";
		}
		
	}
	//get moments获取所有moments
	//参数userId用户Id,currentPage当前页,amount每页显示数量
	//author:pengshizhong
	//merge by miao
	@RequestMapping(value = "getAllMomentVo",method=RequestMethod.GET)
	@ResponseBody
	public Object getAllMomentVo(@RequestParam("userId")String userId,@RequestParam("currentPage")String currentPage
			,@RequestParam("amount")String amount) throws UnsupportedEncodingException
	{
		try {
			Page moments=momentService.findByUserId(userId,currentPage,amount);
			List<Moment> list = moments.getList();
			List<MomentVo> momentVo=new ArrayList();
			for(Moment s:list){
				MomentVo m = new MomentVo();
				User user = s.getUser();
				m.setContent(s.getMomentContent());
				m.setMomentId(s.getMomentId());
				m.setPubTime(s.getMomentPubtime());
				m.setNickName(user.getNickname());
				m.setUserId(user.getUserId());
				momentVo.add(m);
			}
			System.out.println("2");
			moments.setList(momentVo);
			return  moments;
		} catch (Exception e) {
			return "";
			
			
			
			
			
			
			
			
			
			
			
			
			
		}

	}
}
